package zhihui.mobile.ui.myself;

import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.zgm.utils.ui.UiTextHeader;

import zhihui.mobile.R;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.myself.UpdateSexPresenter;
import zhihui.mobile.ui.BaseActivity;

/**
 * Created by 张公铭 on 2017/9/24 0024.
 */

public class UpdateSexActivity extends BaseActivity implements IUpdateSexContract.IUpdateSexView
{
    private IUpdateSexContract.IUpdateSexPresenter sexPresenter = new UpdateSexPresenter();

    private UiTextHeader header;
    private CheckBox cb_boy;
    private CheckBox cb_girl;
    private CheckBox cb_bm;
    private int sex;

    @Override
    public void initView()
    {
        header = (UiTextHeader) findViewById(R.id.header_setting_update_sex);
        header.setBackgroundColor("#4b85e4");
        header.setTitle("性别");
        header.setTitleColor("#ffffff");
        header.setLeftBottonIcon(R.drawable.back);
        header.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goBack(UpdateSexActivity.class);
            }
        });

        header.setRightText("确定");
        header.setRightTextColor("#ffffff");
        header.setRightTextListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //保存
                if (cb_boy.isChecked() || cb_girl.isChecked() || cb_bm.isChecked())
                {
                    sexPresenter.updateSex(sex);
                } else
                {
                    showMessage("请选择性别");
                }
            }
        });

        cb_boy = (CheckBox) findViewById(R.id.cb_setting_update_sex_boy);
        cb_boy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    cb_girl.setChecked(false);
                    cb_bm.setChecked(false);
                    sex = 1;
                }
            }
        });

        cb_girl = (CheckBox) findViewById(R.id.cb_setting_update_sex_girl);
        cb_girl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    cb_boy.setChecked(false);
                    cb_bm.setChecked(false);
                    sex = 0;
                }

            }
        });

        cb_bm = (CheckBox) findViewById(R.id.cb_setting_update_sex_bm);
        cb_bm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    cb_girl.setChecked(false);
                    cb_boy.setChecked(false);
                    sex = 2;
                }

            }
        });

        initCheck();
    }

    private void initCheck()
    {
        sex = UserManager.getInstance().getCurUser().getSex();
        switch (sex)
        {
            case 0:
                cb_girl.setChecked(true);
                break;
            case 1:
                cb_boy.setChecked(true);
                break;
            case 2:
                cb_girl.setChecked(true);
                break;
        }
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.setting_update_sex_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{sexPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        sexPresenter.init(this, this);
    }
}
